package com.nytimes.ohos.store.sample.data.model;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

/**
 * Immutable implementation of {@link Preview}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutablePreview.builder()}.
 */
@SuppressWarnings("all")
public final class ImmutablePreview extends Preview {
  private final ImmutableList<Images> images;

  private ImmutablePreview(ImmutableList<Images> images) {
    this.images = images;
  }

  /**
   * images
   *
   * @return The value of the {@code images} attribute
   */
  @Override
  public ImmutableList<Images> images() {
    return images;
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link Preview#images() images}.
   *
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutablePreview withImages(Images... elements) {
    ImmutableList<Images> newValue = ImmutableList.copyOf(elements);
    return new ImmutablePreview(newValue);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link Preview#images() images}.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   *
   * @param elements An iterable of images elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutablePreview withImages(Iterable<? extends Images> elements) {
    if (this.images == elements) return this;
    ImmutableList<Images> newValue = ImmutableList.copyOf(elements);
    return new ImmutablePreview(newValue);
  }

  /**
   * This instance is equal to all instances of {@code ImmutablePreview} that have equal attribute values.
   *
   * @param another object
   * @return {@code true} if {@code this} is equal to {@code another} instance
   */
  @Override
  public boolean equals(Object another) {
    if (this == another) return true;
    return another instanceof ImmutablePreview
        && equalTo((ImmutablePreview) another);
  }

  private boolean equalTo(ImmutablePreview another) {
    return images.equals(another.images);
  }

  /**
   * Computes a hash code from attributes: {@code images}.
   *
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 31;
    h = h * 17 + images.hashCode();
    return h;
  }

  /**
   * Prints the immutable value {@code Preview} with attribute values.
   *
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return MoreObjects.toStringHelper("Preview")
        .omitNullValues()
        .add("images", images)
        .toString();
  }

  /**
   * Creates an immutable copy of a {@link Preview} value.
   * Uses accessors to get values to initialize the new immutable instance.
   * If an instance is already immutable, it is returned as is.
   *
   * @param instance The instance to copy
   * @return A copied immutable Preview instance
   */
  public static ImmutablePreview copyOf(Preview instance) {
    if (instance instanceof ImmutablePreview) {
      return (ImmutablePreview) instance;
    }
    return ImmutablePreview.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutablePreview ImmutablePreview}.
   *
   * @return A new ImmutablePreview builder
   */
  public static Builder builder() {
    return new Builder();
  }

  /**
   * Builds instances of type {@link ImmutablePreview ImmutablePreview}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
   * but instead used immediately to create instances.</em>
   */
  public static final class Builder {
    private ImmutableList.Builder<Images> images = ImmutableList.builder();

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code Preview} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     * Collection elements and entries will be added, not replaced.
     *
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(Preview instance) {
      Preconditions.checkNotNull(instance, "instance");
      addAllImages(instance.images());
      return this;
    }

    /**
     * Adds one element to {@link Preview#images() images} list.
     *
     * @param element A images element
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addImages(Images element) {
      this.images.add(element);
      return this;
    }

    /**
     * Adds elements to {@link Preview#images() images} list.
     *
     * @param elements An array of images elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addImages(Images... elements) {
      this.images.add(elements);
      return this;
    }

    /**
     * Sets or replaces all elements for {@link Preview#images() images} list.
     *
     * @param elements An iterable of images elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder images(Iterable<? extends Images> elements) {
      this.images = ImmutableList.builder();
      return addAllImages(elements);
    }

    /**
     * Adds elements to {@link Preview#images() images} list.
     *
     * @param elements An iterable of images elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addAllImages(Iterable<? extends Images> elements) {
      this.images.addAll(elements);
      return this;
    }

    /**
     * Builds a new {@link ImmutablePreview ImmutablePreview}.
     *
     * @return An immutable instance of Preview
     * @throws IllegalStateException if any required attributes are missing
     */
    public ImmutablePreview build() {
      return new ImmutablePreview(images.build());
    }
  }
}
